import {
  list
} from '../../api/blog'
import {
  all
} from '../../api/carousel'


Component({
  data: {
    form: {
      orderType: 1,
      pageNum: 1,
      pageSize: 10
    },
    orderTypeNewStyle: true,
    orderTypeHotStyle: false,
    menu: {},
    arr: [],
    reachButtom: false,
    sk: true,
    carouselList: []
  },
  pageLifetimes: {
    show() {
      if (typeof this.getTabBar === 'function' &&
        this.getTabBar()) {
        this.getTabBar().setData({
          selected: 0
        })
      }
      // let menu = wx.getMenuButtonBoundingClientRect() 获取按钮信息
      this.setData({
        menu: wx.getMenuButtonBoundingClientRect()
      })


    },
    attached() {
      console.log('wohaole ')
    }
  },
  methods: {
    handleOrder(e) {
      const type = e.currentTarget.dataset.type
      this.setData({
        form: {
          orderType: type,
          pageNum: 1
        },
        reachButtom: false,
        // arr: [],
        orderTypeNewStyle: (type === 1),
        orderTypeHotStyle: (type === 2)
      })
      // ScrollViewContext.scrollTo({
      //   top: 0
      // })
      // wx.pageScrollTo({
      //   selector: '.sv-box',
      //   scrollTop: 0,
      //   duration: 300
      // })
      list(this.data.form).then(res => {
        this.setData({
          arr: res.records
        })
      })
    },
    handleButtom() {
      if (this.data.reachButtom) {
        return false;
      }
      // wx.showLoading({
      //   title: '加载中',
      //   mask: true
      // })
      this.data.form.pageNum = this.data.form.pageNum + 1
      this.setData({
        form: this.data.form
      })
      list(this.data.form).then(res => {
        this.setData({
          arr: this.data.arr.concat(res.records)
        })
        // wx.hideLoading()

        if (res.total === this.data.arr.length) {
          this.setData({
            reachButtom: true
          })
        }
      })
    },
    toDetail(e) {
      const id = e.currentTarget.dataset.id
      wx.navigateTo({
        url: `/pages/detail/index?id=${id}`,
      })
    },
    toSearch() {
      wx.navigateTo({
        url: `/pages/list/index`,
      })
    }
  },
  attached: function () {
    // 每次刷新重新设置为第一页
    this.data.form.pageNum = 1
    this.setData({
      reachButtom: false
    })
    list(this.data.form).then(res => {
      this.setData({
        arr: res.records,
        sk: false
      })
    })
    all().then(res => {
      this.setData({
        carouselList: res
      })
    })
  }
})